{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "#!/bin/python3\n",
    "import ctypes\n",
    "from ctypes import *\n",
    "import numpy as np\n",
    "import time\n",
    "import cv2 as cv\n",
    "from matplotlib import pyplot as plt\n",
    "import SocketTransfer\n",
    "from libqhy import *\n",
    "qhyccd = CDLL('/usr/local/lib/libqhyccd.so')\n",
    "qhyccd.GetQHYCCDParam.restype=c_double\n",
    "qhyccd.OpenQHYCCD.restype=ctypes.POINTER(c_uint32)\n",
    "# ref: https://www.qhyccd.com/bbs/index.php?topic=6356.0\n",
    "# H.F. @20191115"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Python Knowledge\n",
    "+ https://docs.python.org/3/library/ctypes.html\n",
    "\n",
    "QHY POST\n",
    "+ https://www.qhyccd.com/bbs/index.php?topic=6316.0\n",
    "+ https://www.qhyccd.com/bbs/index.php?topic=6356.0\n",
    "+ https://www.qhyccd.com/bbs/index.php?topic=6663.0\n",
    "+ https://github.com/EastEriq/QHYccd-matlab\n",
    "+ https://note.youdao.com/coshare/index.html?token=AD62E2033F534E7CB3607CEC405CB3B3&gid=7195236?auto"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create server object\n",
    "server = SocketTransfer.socket_sender()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Waiting receive program\n",
      "No receive program found!\n",
      "Please reconnect after opening reveive program\n"
     ]
    }
   ],
   "source": [
    "server.accept()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b'QHY163M-8cf4dc43a8fe4f8f'\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ret = -1\n",
    "qhyccd.InitQHYCCDResource()\n",
    "qhyccd.ScanQHYCCD()\n",
    "type_char_array_32 = c_char*32\n",
    "id=type_char_array_32()\n",
    "qhyccd.GetQHYCCDId(c_int(0), id)    # open the first camera\n",
    "print(id.value)\n",
    "cam = qhyccd.OpenQHYCCD(id)\n",
    "qhyccd.SetQHYCCDStreamMode(cam, 1)  # 0 for single frame\n",
    "qhyccd.InitQHYCCD(cam)\n",
    "\n",
    "# Get Camera Parameters\n",
    "chipw = c_double()\n",
    "chiph = c_double()\n",
    "w = c_uint()\n",
    "h = c_uint()\n",
    "pixelw = c_double()\n",
    "pixelh = c_double() \n",
    "bpp = c_uint()\n",
    "channels = c_uint32(1)\n",
    "qhyccd.GetQHYCCDChipInfo(cam, byref(chipw), byref(chiph), byref(w), byref(h),\n",
    "                         byref(pixelw), byref(pixelh), byref(bpp))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Capture Live Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "28.6"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "qhyccd.SetQHYCCDParam(cam, CONTROL_ID.CONTROL_USBTRAFFIC, c_double(60)) # don't work\n",
    "qhyccd.SetQHYCCDParam(cam, CONTROL_ID.CONTROL_TRANSFERBIT, c_double(16))\n",
    "qhyccd.SetQHYCCDParam(cam, CONTROL_ID.CONTROL_MANULPWM, c_double(255))\n",
    "qhyccd.SetQHYCCDParam(cam, CONTROL_ID.CONTROL_COOLER, c_double(-15))\n",
    "qhyccd.GetQHYCCDParam(cam, CONTROL_ID.CONTROL_CURTEMP)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "qhyccd.SetQHYCCDParam(cam, CONTROL_ID.CONTROL_USBTRAFFIC, c_double(10)) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10.0"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "qhyccd.GetQHYCCDParam(cam, CONTROL_ID.CONTROL_USBTRAFFIC) # don't work"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "28.8"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "qhyccd.GetQHYCCDParam(cam, CONTROL_ID.CONTROL_CURTEMP)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "qhyccd.StopQHYCCDLive(cam)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 "
     ]
    },
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "exposure = c_double(0.001 * 10e6) # us\n",
    "interval = 1 #.4s #2.5 fps\n",
    "duration = 1*60 #s\n",
    "time_start = time.time()\n",
    "time_last = time.time()\n",
    "time_now = time.time()\n",
    "counter = 0\n",
    "roi_w = c_uint(3522)\n",
    "roi_h = c_uint(3522)\n",
    "qhyccd.SetQHYCCDResolution(cam, 0, 0, roi_h, roi_w)\n",
    "qhyccd.SetQHYCCDParam(cam, CONTROL_ID.CONTROL_EXPOSURE, exposure) # unit: us\n",
    "imgdata = (ctypes.c_uint16 * roi_h.value * roi_w.value)()\n",
    "\n",
    "qhyccd.BeginQHYCCDLive(cam)\n",
    "while ( time_now - time_start < duration + interval ): # add one more interval\n",
    "    while (time_now - time_last > interval) :\n",
    "        qhyccd.GetQHYCCDLiveFrame(\n",
    "            cam, byref(roi_h), byref(roi_w), byref(bpp), byref(channels), imgdata)\n",
    "        img = np.asarray(imgdata).reshape([roi_w.value, roi_h.value])\n",
    "        server.send_img(img)\n",
    "        #cv.imwrite(\"%05d.tiff\"%counter, img)\n",
    "        print(counter, end=\" \")\n",
    "        counter += 1\n",
    "        time_last = time_now\n",
    "    time_now = time.time()\n",
    "qhyccd.StopQHYCCDLive(cam)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "exposure = c_double(0.001 * 10e6) # us\n",
    "interval = 0.3 #.4s #2.5 fps\n",
    "duration = 1*60 #s\n",
    "time_start = time.time()\n",
    "time_last = time.time()\n",
    "time_now = time.time()\n",
    "counter = 0\n",
    "roi_w = c_uint(3522)\n",
    "roi_h = c_uint(3522)\n",
    "qhyccd.SetQHYCCDResolution(cam, 0, 0, roi_h, roi_w)\n",
    "qhyccd.SetQHYCCDParam(cam, CONTROL_ID.CONTROL_EXPOSURE, exposure) # unit: us\n",
    "imgdata = (ctypes.c_uint16 * roi_h.value * roi_w.value)()\n",
    "\n",
    "test = np.uint16(np.random.normal(size=(3522, 3522)))\n",
    "\n",
    "qhyccd.BeginQHYCCDLive(cam)\n",
    "while ( time_now - time_start < duration + interval ): # add one more interval\n",
    "    while (time_now - time_last > interval) :\n",
    "        #hyccd.GetQHYCCDLiveFrame(\n",
    "        #   cam, byref(roi_h), byref(roi_w), byref(bpp), byref(channels), imgdata)\n",
    "        #mg = np.asarray(imgdata).reshape([roi_w.value, roi_h.value])\n",
    "        #server.send_img(img)\n",
    "        server.send_img(test)\n",
    "        #cv.imwrite(\"%05d.tiff\"%counter, img)\n",
    "        print(counter, end=\" \")\n",
    "        counter += 1\n",
    "        time_last = time_now\n",
    "    time_now = time.time()\n",
    "qhyccd.StopQHYCCDLive(cam)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "qhyccd.CloseQHYCCD(cam)\n",
    "qhyccd.ReleaseQHYCCDResource()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
